package com.audaque.springboot.foshanupload.sessionvalid.config;


import com.audaque.springboot.foshanupload.authcore.properties.LoginProperties;
import com.audaque.springboot.foshanupload.core.constants.ComConst;
import com.audaque.springboot.foshanupload.core.properties.StaticResourcePathProperties;

import com.audaque.springboot.foshanupload.sessionvalid.filter.SessionFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @author zgb
 * @desc filter用于认证和授权
 * @date 2022-01-25 22:24:15
 */
@Configuration
public class RedisHttpSessionValidFilterConfig {

    @Autowired
    private LoginProperties loginProperties;
    @Autowired
    private StaticResourcePathProperties staticResourcePathProperties;


    /**
     * 配置过滤器
     * order属性:控制过滤器加载顺序：数字越小，加载越早
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean sessionFilterRegistration() {
        //新建过滤器注册类
        FilterRegistrationBean<SessionFilter> registration = new FilterRegistrationBean<SessionFilter>();
        // 添加我们写好的过滤器
        //DelegatingFilterProxy就是一个对于servlet filter的代理，用这个类的好处主要是通过Spring容器来管理servlet filter的生命周期，还有就是如果filter中需要一些Spring容器的实例，可以通过spring直接注入，另外读取一些配置文件这些便利的操作都可以通过Spring来配置实现。
        registration.setFilter(new SessionFilter());
        registration.setName("sessionFilter");
        // 设置过滤器的URL模式
        registration.addUrlPatterns(ComConst.FILTER_ANY_MATCH);

        //要排除的路径
       List<String> excludePathPatterns = staticResourcePathProperties.getList();
        List<String> unAuthenticatedUrlList = loginProperties.getUnAuthenticatedUrlList();
        unAuthenticatedUrlList.addAll(excludePathPatterns);
        String excludedUris = String.join(",", unAuthenticatedUrlList);
        //自定义参数
        registration.addInitParameter("excludedUris", excludedUris);//需要排除的uri

        //控制过滤器加载顺序：数字越小，加载越早
        registration.setOrder(0);
        return registration;
    }


}
